public class Solution331 {
    public boolean isValidSerialization(String preorder) {
        String[] ev=preorder.split(",");
        try {
            if (dfs(0, ev) == ev.length - 1) {
                return true;
            } else {
                return false;
            }
        }catch (Exception e){
            return false;
        }
    }

    int dfs(int start,String[] s){
        if (s[start].equals("#")){
            return start;
        }
        int t1=dfs(start+1,s);
        int t2=dfs(t1+1,s);
        return t2;
    }

    public static void main(String[] args) {
        System.out.println(new Solution331().isValidSerialization("9,#,#,1"));
    }
}
